﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Content.CDN;

namespace Marbles.Web.Admin.Content
{
    public partial class CDN : ContentBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadGroups();
            LoadData();
        }

        private void LoadData()
        {
            cGrdCDN.DataSource = new CDNSettingCollection();
            cGrdCDN.DataBind();
        }

        private void LoadGroups()
        {
            if (cDrpGroup.Items.Count <= 0)
            {
                cDrpGroup.DataSource = CDNSettingCollection.GetSupportedCDNTypes();
                cDrpGroup.DataBind();
            }
        }

        protected void Page_Command(object sender, CommandEventArgs e)
        {
            CDNSetting setting = null;

            switch (e.CommandName)
            {
                case "Modify":                    
                    cDlgCDNEdit.Visible = true;
                    cDlgCDNEdit.Title = "Edit Existing CDN";
                    setting = CDNSettingCollection.Load(e.CommandArgument.ToString());
                    cDrpGroup.ClearSelection();
                    cDrpGroup.SelectedValue = setting.Name;
                    cTxtUrl.Text = setting.Url;
                    cChkDefault.Checked = setting.DefaultCDN;
                    cLblId.Text = setting.Id;
                    cDrpType.SelectedValue = setting.Type;
                    cBtnSave.CommandArgument = setting.Id;
                    cBtnDelete.CommandArgument = setting.Id;
                    cBtnSave.CommandName = "Save";
                    cBtnDelete.Visible = true;
                    cDrpGroup.Enabled = false;
                    break;
                case "Save":
                    setting = CDNSettingCollection.Load(e.CommandArgument.ToString());
                    setting.Url = cTxtUrl.Text;
                    setting.DefaultCDN = cChkDefault.Checked;
                    setting.Type = cDrpType.SelectedValue;
                    CDNSettingCollection.Save(setting);
                    cBtnSave.CommandArgument = "";
                    cBtnSave.CommandName = "Create";
                    cDrpGroup.Enabled = false;
                    cDlgCDNEdit.Visible = false;
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    LoadData();
                    break;
                case "Delete":
                    setting = CDNSettingCollection.Load(e.CommandArgument.ToString());
                    CDNSettingCollection.Delete(setting);
                    cDlgCDNEdit.Visible = false;
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    LoadData();
                    break;
                case "Cancel":
                    cBtnSave.CommandArgument = "";
                    cBtnSave.CommandName = "Create";
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    cDlgCDNEdit.Visible = false;
                    break;
                case "New":
                    cDlgCDNEdit.Visible = true;
                    cDlgCDNEdit.Title = "Create new CDN";
                    cDrpGroup.ClearSelection();
                    cTxtUrl.Text = "";
                    cChkDefault.Checked = false;
                    cLblId.Text = "";
                    cDrpType.SelectedValue = "text/javascript";
                    cDrpGroup.Enabled = true;
                    cBtnDelete.Visible = false;
                    cBtnSave.CommandArgument = "";
                    cBtnSave.CommandName = "Create";
                    LoadData();
                    break;
                case "Create":
                    setting = CDNSetting.Create(cDrpGroup.SelectedValue);
                    setting.DefaultCDN = cChkDefault.Checked;
                    setting.Url = cTxtUrl.Text;
                    setting.Type = cDrpType.SelectedValue;
                    CDNSettingCollection.Save(setting);
                    cDlgCDNEdit.Visible = false;
                    cDrpGroup.Enabled = true;
                    cBtnSave.CommandArgument = "";
                    cBtnSave.CommandName = "Create";
                    cBtnDelete.CommandArgument = "";
                    cBtnSave.CommandArgument = "";
                    LoadData();
                    break;
            }
        }
    }
}